﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;

using RecommenderSystem.misc;

namespace RecommenderSystem.algorithms.memoryBased
{
    class BasicDT : MemoryBased
    {

        private IDictionary<string, DecisionTree> trees;
        private ICollection<User> lUsers;


        public BasicDT(Dictionary<string, User> users, ICollection<IDataRecord> dataset): base(users, dataset)
        {
            trees = new ConcurrentDictionary<string, DecisionTree>();
            lUsers = users.Values.ToList();

            //Construct for each item decision tree:
            var lItems = dataset.Select(x => x.getItemID()).Distinct().ToList();

            foreach (string itemId in lItems)
            {
                Console.WriteLine("Constructing tree for item " + itemId);
                DecisionTree result = new DecisionTree(lUsers, itemId);
                trees.Add(itemId, result);
            }

        }

        public override double predictRating(string userID, string itemID)
        {

            if (!trees.ContainsKey(itemID))//Constructs item tree
            {
                trees.Add(itemID, constructTree(itemID));
            }

            return predictRating(trees[itemID], userID);
        }

        public override double calculateSimilarity(User user, User otherUser)
        {
            return 0d;
        }



        private double predictRating(DecisionTree itemTree, string userId)
        {
            return itemTree.getPrediction(this._users[userId]);
        }

        private DecisionTree constructTree(string itemId)
        {
            DecisionTree result = new DecisionTree(lUsers, itemId);
            return result;
        }
    }
}
